// pages/login/Caballero-SureExpress/Caballero-SureExpress.js
import common from "../../../utils/public.js";
const db=wx.cloud.database()


Page({

  /**
   * 页面的初始数据
   */
  data: {

    winWidth:0,
    winHeight:0,
    currentTab:0,
    // 用户未签收
    sum:0,
    // 当前日期共取件
    sum22:0,
    //用户已签收
    sumxx:0,
    //快递用户的出现
    noworder:false,
    // 为签收用户
    datalist:[],
    //签收用户
    datalist1:[],
    // 今天的日期
    nowtime:"",
    caballeroName:"",
    // 骑手名字
    realname:"",
    nowtime:"",
    searchtime:"今天",
    search:"",
    Total:0,
    dayuhaoImage:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/dayuhao.png",
    sousuoImage:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/soushuo.png"
  },

  //获取用户框的内容
  search(res){
    var that=this
    that.setData({
      search:res.detail.value
    })
  },

  //将时间进行装换
  changetime(){
    var that=this
    var nowtime=that.data.nowtime
    var nos=""
    for(var i=0;i<nowtime.length;i++){
      if(i==4||i==7){
        nos=nos+'/'
      }else if(i==10){
      }
      else{
        nos=nos+nowtime.charAt(i)
      }
    }
    return nos;
  },

  // 点击获取事件
  sea(){
    var that=this
    if(that.data.search==""){
     var nos=that.changetime()
      that.getexpressNonesure(that.data.openid,"配送中",nos)
      that.getexpressNonesure(that.data.openid,"已送达",nos)
    }else{
      that.setData({
      searchtime:that.data.search
     })
     that.getexpressNonesure(that.data.openid,"配送中",that.data.searchtime)
     that.getexpressNonesure(that.data.openid,"已送达",that.data.searchtime)
   
    
    }
   that.setData({
      noworder:true,
     })
   },

  //获取骑手的姓名、今天的日期
  getcaballeroTime(){
    var that=this
    wx.cloud.callFunction({
      name:'login',
      success(res){
        db.collection('user').where({
          openid:res.result.openid
          }).get().then(ress=>{
          that.setData({
            realname:ress.data[0].realname
          })
        })
      }
    })   

    var nowtime= Date.parse(new Date())/1000
    that.setData({
      nowtime:common.getMyData(nowtime,"Y年m月d日")
    })
  },


  //获取该骑手所取到的快件,page=0,num=5 ="2020/10/29"未配送
  getexpressNonesure(caballero,state="配送中",textdata){
     var that=this

     if(state=="配送中"){
        that.setData({
         sum:0
       })
     }else if(state=="已送达"){
      that.setData({
        sumxx:0
      })
     }

     wx.cloud.callFunction({
      name:'expressget-caballero',
      data:{
        caballero:caballero,
        state:state
      },
      success(res){
        if(res.result.data.length==0){
          wx.showToast({
            title: '已无跟多数据',
            icon:'none',
            duration:2000
          })
        }else if(res.result.data.length==0&&that.data.dataset.length==0){
          wx.showModal({
            title: '提示',
            content: '今天没有未签收的快递',
            success (res) {
              if (res.confirm) {
              } else if (res.cancel) {
              }
            }
          })
        }else{
          // var olddata=that.data.datalist  olddata.concat()
          var newdate=res.result.data
          newdate.forEach(item=>{
            if(typeof(item.time)=="number"){
               if(that.textTSS(textdata,item.caballero_time*1000)){
                item.time=common.getMyData(item.time,"Y-m-d H:i:s")
                if(state=="配送中"){
                  that.setData({
                  sum:that.data.sum+1
                 })
                
                }else if(state=="已送达"){
                  that.setData({
                    sumxx:that.data.sumxx+1
                   })
                }
               
              }
            }
          })

        
          for(var i=0;i<newdate.length;i++){
            if(typeof(newdate[i].time)=='number'){
               newdate.splice(i,1)
               i=i-1
            }
          }

          if(state=="配送中"){
            that.setData({
             datalist:newdate,
             Total:that.data.sum+that.data.sumxx
           })
         }else if(state=="已送达"){
          that.setData({
            datalist1:newdate,
            Total:that.data.sum+that.data.sumxx
          })
         }
        }
       }
    })
  },


// .orderBy('time','desc')
   // 滑动切换tab
   bindChange:function(e){
    
    var that=this;
    that.setData({currentTab:e.detail.current});
    
  },
// 点击tab切换
  swichNav:function(e){
    var that=this;
    if(this.data.currentTab===e.target.dataset.current){
      return false;
    }else{
      that.setData({
        currentTab:e.target.dataset.current
      })
    }
  },

  //日期格式装换为时间戳进行比较
  textTSS(textdata,TSS){
    var that=this
    if(that.checkDate(textdata)){
      var txtdata=new Date(textdata).getTime()
      if(new Date(txtdata).toDateString()==new Date(TSS).toDateString()){
           return true;
      }
    }
  },
  
  //日期格式的校验
  checkDate(dateStr){
    var a = /^(\d{4})\/(\d{2})\/(\d{2})$/
    if (!a.test(dateStr)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的日期格式：yyyy/mm/dd',
        success (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
      return false
    }else{
        return true;
    }
},

//用户点击快递
noworderBody(res){

  var id=res.currentTarget.dataset.id
  wx.navigateTo({
    url: '/pages/login/administrator-exprssorder/exprss-order/exprss-order?expressId='+id,
  })
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading({
      title: '数据加载中...',
    })


    var that=this;
    // 获取系统信息：手机的宽度和高度
    wx.getSystemInfo({
      success: function(res){
        that.setData({
          winWidth:res.windowWidth,
          winHeight:res.windowHeight
        });
      }
    });
     that.getcaballeroTime()
     var nos=that.changetime()
     that.getexpressNonesure(that.data.openid,"已送达",nos)
     that.getexpressNonesure(that.data.openid,"配送中",nos)
     wx.hideLoading()
    //  that.setData({
    //    Total:that.data.sum+that.data.sumxx
    //  })
    // this.textTSS("2020/10/29", 1603976459*1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})